import unittest
import random
from alg import Solution
from gen import generate_test_case

class TestFindDuplicate(unittest.TestCase):
    def setUp(self):
        self.solution = Solution()

    def test_example1(self):
        nums = [1, 3, 4, 2, 2]
        self.assertEqual(self.solution.findDuplicate(nums), 2)

    def test_example2(self):
        nums = [3, 1, 3, 4, 2]
        self.assertEqual(self.solution.findDuplicate(nums), 3)

    def test_all_same(self):
        nums = [5, 5, 5, 5, 5]
        self.assertEqual(self.solution.findDuplicate(nums), 5)

    def test_min_size(self):
        nums = [1, 1]
        self.assertEqual(self.solution.findDuplicate(nums), 1)

    def test_random_generated(self):
        for _ in range(100):  # 生成100个随机测试用例
            n = random.randint(1, 1000)
            nums = generate_test_case(n)
            # 计算预期结果（排序后检查相邻元素）
            sorted_nums = sorted(nums)
            for i in range(len(sorted_nums)-1):
                if sorted_nums[i] == sorted_nums[i+1]:
                    expected = sorted_nums[i]
                    break
            self.assertEqual(self.solution.findDuplicate(nums), expected)

if __name__ == '__main__':
    unittest.main()